package com.autotest.glin.common;

import com.autotest.glin.util.Constant;
import io.restassured.RestAssured;
import io.restassured.response.Response;
import org.testng.Reporter;

import java.util.Map;

/**
 * post请求
 */
public class RunCasePost {

    /**
     * 传token的post请求
     *
     * @param url
     * @param path
     * @param bodyString
     * @param accessToken
     * @return Response
     */
    public Response runCase(String url, String path, String accessToken, String bodyString) {
        Response response = null;
        String uri = url + path;
        Reporter.log("Post request url: " + url + "  path:" + path);
        Reporter.log("Post accessToken : " + accessToken);
        Reporter.log("Post body:" + bodyString);
        response = RestAssured.given().header("accessToken", accessToken)
                .contentType("application/json;charset=UTF-8").request()
                .body(bodyString).post(uri);
        Reporter.log("    response: " + response.asString());
        return response;
    }

    /**
     * 传hearders 的post请求
     *
     * @param url
     * @param path
     * @param bodyString
     * @param headers
     * @return Response
     */
    public Response post(String url, String path, String bodyString, Map<String, Object> headers) {
        Reporter.log("Post request url: " + url + "  path:" + path);
        Reporter.log("Post headers : " + headers);
        Reporter.log("Post body:" + bodyString);
        Response response = RestAssured.given().basePath(path)
                .headers(headers)
                .request()
                .body(bodyString).post(url);
        if (response.statusCode() != Constant.STATE_CODE_OK) {
            Reporter.log("request response: " + response.getBody().asString());
        }
        Reporter.log("    response: " + response.asString());
        return response;
    }


}
